const chineseMonthName = ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
const monthName = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
const dayNameList = [{
        chineseFullName: "周日",
        chineseShortName: "日",
        fullName: "Sunday",
        shortName: "Sun",
        dayNumber: 0,
    },
    {
        chineseFullName: "周一",
        chineseShortName: "一",
        fullName: "Monday",
        shortName: "Mon",
        dayNumber: 1,
    },
    {
        chineseFullName: "周二",
        chineseShortName: "二",
        fullName: "Tuesday",
        shortName: "Tue",
        dayNumber: 2,
    },
    {
        chineseFullName: "周三",
        chineseShortName: "三",
        fullName: "Wednesday",
        shortName: "Wed",
        dayNumber: 3,
    },
    {
        chineseFullName: "周四",
        chineseShortName: "四",
        fullName: "Thursday",
        shortName: "Thu",
        dayNumber: 4,
    },
    {
        chineseFullName: "周五",
        chineseShortName: "五",
        fullName: "Friday",
        shortName: "Fri",
        dayNumber: 5,
    },
    {
        chineseFullName: "周六",
        chineseShortName: "六",
        fullName: "Saturday",
        shortName: "Sat",
        dayNumber: 6,
    }
]
/**
 * 获取日历数据
 * @param {Date} modelValue - 时间日期 
 * @param {Number} minValue - 最小年份
 * @param {Number} maxValue - 最大年份 
 * @returns 
 */
export const constsetCalendarData = (modelValue, minValue, maxValue) => {
    const calendarProps = {
        target: {
            year: null,
            month: null,
            date: null,
            day: null,
            monthShortName: null,
            monthFullName: null,
            monthChineseFullName: null,
            firstDay: null,
            firstDayIndex: null,
            totalDays: null,
        },
        previous: {
            totalDays: null,
        }
    }
    // 获取目标日期的年月日星期几数据
    const year = modelValue.getFullYear();
    const month = modelValue.getMonth();
    const date = modelValue.getDate();
    const day = modelValue.getDay();

    calendarProps.target.year = year
    calendarProps.target.month = month
    calendarProps.target.date = date
    calendarProps.target.day = day

    if (
        year < minValue ||
        year > maxValue
    ) {
        console.error("无效的年份，请检查传入的数据是否是正常");
        return;
    }

    // 获取到目标日期的月份【中文】名称
    let dateString = modelValue.toString().split(" ");
    calendarProps.target.monthShortName = dateString[1];
    calendarProps.target.monthFullName = monthName[month];
    calendarProps.target.monthChineseFullName = chineseMonthName[month];
    // 获取目标月份的第一天是星期几，和在星期几中的索引值
    const targetMonthFirstDay = new Date(year, month, 1);
    calendarProps.target.firstDay = targetMonthFirstDay.getDay();
    calendarProps.target.firstDayIndex = dayNameList.findIndex(
        (day) => day.dayNumber === calendarProps.target.firstDay
    );

    // 获取目标月份总共多少天
    const targetMonthLastDay = new Date(year, month + 1, 0);
    calendarProps.target.totalDays = targetMonthLastDay.getDate();

    // 获取目标月份的上个月总共多少天
    const previousMonth = new Date(year, month, 0);
    calendarProps.previous.totalDays = previousMonth.getDate();

    let i;
    let dates = 1;
    const originData = [];
    const firstRow = [];

    for (i = 0; i <= 6; i++) {
        if (i < calendarProps.target.firstDayIndex) {
            const previousDate = calendarProps.previous.totalDays - calendarProps.target.firstDayIndex + (i + 1);

            const dateObjs = new Date(calendarProps.target.year, calendarProps.target.month - 1,previousDate)

            firstRow.push({
                dateObj: dateObjs,
                dateNumber: previousDate,
                dateType: "previous",
            });
        }else {
            const dateObjs = new Date(calendarProps.target.year, calendarProps.target.month, dates)

            // 设置目标月份当月的日期数据
            firstRow.push({
                dateObj: dateObjs,
                dateNumber: dates,
                dateType: "current",
            });
            dates++;
        }
    }
    originData.push(firstRow);

    for (let j = 0; j <= 4; j++) {
        const rowData = [];
        for (let k = 0; k <= 6; k++) {
            if (date <= calendarProps.target.totalDays) {
                const dateObjs = new Date(calendarProps.target.year, calendarProps.target.month, dates)

                rowData.push({
                    dateObj: dateObjs,
                    dateNumber: dates,
                    dateType: "current",
                });
            }else {
                // 设置目标月份下个月的日期数据
                const nextDate = dates - calendarProps.target.totalDays;

                const dateObjs = new Date(calendarProps.target.year, calendarProps.target.month + 1,nextDate)

                rowData.push({
                    dateObj:dateObjs,
                    dateNumber: nextDate,
                    dateType: "next",
                });
            }
            dates++;
        }
        originData.push(rowData);
    }

    return {
        calendarProps,
        originData
    }
}